# -*- coding:utf-8 -*-
__author__ = 'weikai'
from request_data import query_car,query_detail
from parse import parse_query_car,conver_time
from dbmap import  sql_add_usercompay2,del_car
import traceback
from log import Logger
log=Logger()
def cjbx_start(palte_number):
    try:
        out=query_detail(palte_number)
        if out!=0:
            #获取车辆详细信息
            html=query_car(palte_number)
            out=parse_query_car(html)
            alldata=[]
            jq=out.get("jq",[])
            if len(jq)!=0:
                jq=jq[0]

            sy=out.get("sy",[])
            if len(sy)!=0:
                sy=sy[0]
            sy_chuxian=out.get("sy_chuxian",[])
            if len(sy_chuxian)!=0:
                sy_chuxian=sy_chuxian[0]
            else:
                sy_chuxian={}
            jq_chuxian=out.get("jq_chuxian",[])
            if len(jq_chuxian)!=0:
                jq_chuxian=jq_chuxian[0]
            else:
                jq_chuxian={}
            if  len(jq)!=0:
                alldata.append(jq)
            if  len(sy)!=0:
                alldata.append(sy)
            if  len(sy_chuxian)!=0:
                alldata.append(sy_chuxian)
            if  len(jq_chuxian)!=0:
                alldata.append(jq_chuxian)
            if len(alldata)!=0:
                alldata.sort(key=lambda obj:obj.get('end_time'),reverse=True)
                first=alldata[0]
                first['end_time']=conver_time(first['end_time'])
                sql_add_usercompay2(first)


    except Exception,e:
        log.error(e)
        log.error(traceback.format_exc())

if __name__=="__main__":
    cjbx_start("苏MCD777")